package controllers

import (
	"net/http"

	"fmt"


	"github.com/gin-gonic/gin"


	"git.oschina.net/YPTArch/apigateway/model"

	"git.oschina.net/YPTArch/apigateway/lib"
	"git.oschina.net/YPTArch/apigateway/sys"
	"git.oschina.net/YPTArch/apigateway/conf"
)


//GetRouter ...
func GetRouter() *gin.Engine {
	router := gin.New()
	router.GET("/api", func(c *gin.Context) {
		c.String(http.StatusOK, "welcome to apigateway")
	})
	router.POST("/api", func(c *gin.Context) {
		c.String(http.StatusOK, "welcome to apigateway")
	})

	router.GET("/api/:ms", func(c *gin.Context) {
		msName := c.Param("ms")
		msg := fmt.Sprint("please input microserver Api----Get ", msName)
		c.String(http.StatusOK, msg)
	})
	router.POST("/api/:ms", func(c *gin.Context) {
		msName := c.Param("ms")
		msg := fmt.Sprint("please input microserver Api----Post ", msName)
		c.String(http.StatusOK, msg)
	})
	router.GET("/usercenter/login", loginMethods)
	router.GET("/usercenter/logout", logoutMethods)
	router.POST("/usercenter/login", loginMethods)
	router.POST("/usercenter/logout", logoutMethods)

	router.GET("/api/:ms/:api", requestHandles)
	router.POST("/api/:ms/:api", requestHandles)
	return router
}


func loginMethods(c *gin.Context) {
	urlHost := conf.APPAuthUrl()+"/login?" + c.Request.URL.RawQuery
	data := c.Request.Body
	resModel := model.GetModelsByUrl(urlHost, "POST", data, c.Request.Header)
	if resModel == nil {
		c.JSON(http.StatusOK, &model.Result{model.InternalServerError, "服务内部请求失败", nil})
	}
	c.JSON(http.StatusOK, resModel)
	return
}

func logoutMethods(c *gin.Context) {
	urlParams := c.Request.URL.RawQuery
	urlHost := conf.APPAuthUrl()+"/logout?" + urlParams
	data := c.Request.Body
	resModel := model.GetModelsByUrl(urlHost, "POST", data, c.Request.Header)
	if resModel == nil {
		c.JSON(http.StatusOK, &model.Result{model.InternalServerError, "服务内部请求失败", nil})
	}
	c.JSON(http.StatusOK, resModel)
	return
}

func requestHandles(c *gin.Context) {
	urlParams := c.Request.URL.RawQuery

	params := lib.GetParams(c)

	temp := sys.GetMsInfo(params["MS"], params["API"])

	if temp == nil {
		c.JSON(http.StatusOK, &model.Result{model.InternalServerError, "服务器内部错误", nil})
		return
	}

	if temp.MSHost == "" {
		c.JSON(http.StatusOK, &model.Result{model.InternalServerError, "服务未找到", nil})
		return
	}
	if temp.APIMethods == "" {
		c.JSON(http.StatusOK, &model.Result{model.InternalServerError, "API未找到", nil})
		return
	}

	urlHost := temp.MSHost + "/" + temp.MsName + "/" + temp.APIName + "?" + urlParams

	data := c.Request.Body

	if temp.TokenMethods == "1" {
		//请求AUTH
		authRes := model.GetModelsByUrl(conf.APPAuthUrl()+"/validate?"+urlParams, "POST", data, c.Request.Header)
		if authRes == nil {
			c.JSON(http.StatusOK, &model.Result{model.InternalServerError, "AUTH认证失败", nil})
			return
		}
		if authRes.Code == http.StatusOK {
			resModel := model.GetModelsByUrl(urlHost, temp.APIMethods, data, c.Request.Header)
			if resModel == nil {
				c.JSON(http.StatusOK, &model.Result{model.InternalServerError, "服务内部请求错误", nil})
				return
			}
			c.JSON(http.StatusOK, resModel)
			return
		}
		c.JSON(http.StatusOK, authRes)
		return
	}
	//直接请求微服务
	resModel := model.GetModelsByUrl(urlHost, temp.APIMethods, data, c.Request.Header)
	if resModel == nil {
		c.JSON(http.StatusOK, &model.Result{model.InternalServerError, "服务内部请求错误", nil})
		return
	}
	c.JSON(http.StatusOK, resModel)
	return
}










